﻿using MesGLApp.BLL.Role;
using MesGLApp.DAL.RoleDB;
using MesGLApp.DAL.RoleDTO;
using MesGLApp.Tools;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Forms;

namespace MesGLApp.View
{
    partial class LoginFrm : Smobiler.Core.Controls.MobileForm
    {
        #region "内部变量"
        private HomeControler _ctrl = new HomeControler();
        private static BaseUserDTO _login_user = null;
        private static BaseProgModel _prog_info = null;
        private static List<BaseRoleModel> _role_list = new List<BaseRoleModel>();
        private static List<BaseMenuDTO> _menu_list = new List<BaseMenuDTO>();

        

        /// <summary>
        /// 登陆用户
        /// </summary>
        public static BaseUserDTO LoginUser
        {
            get { return  _login_user; }
        }

        /// <summary>
        /// 登陆用户的角色
        /// </summary>
        public static List<BaseRoleModel> RoleList
        {
            get { return _role_list; }
        }

        /// <summary>
        /// 用户可以操作的界面
        /// </summary>
        public static List<BaseMenuDTO> MenuList
        {
            get { return _menu_list; }
        }

        /// <summary>
        /// 项目名称
        /// </summary>
        public static BaseProgModel ProgInfo
        {
            get { return _prog_info; }
        }
        #endregion

        public LoginFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        #region "控件事件"
        /// <summary>
        /// 用户忘记密码时的提示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnForget_Press(object sender, EventArgs e)
        {
            MessageBox.Show("请联系IT部！");
        }

        /// <summary>
        /// 进入登陆界面时，检查权限系统是否有该工程存在
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginFrm_Load(object sender, EventArgs e)
        {
            string progName = "微观MESApp";
            _prog_info = this._ctrl.SearchProgInfoByName(progName);
            if (_prog_info == null || _prog_info.id <= 0)
            {
                MessageBox.Show("工程读取失败，请联系IT部！", "异常");
                this.btnLogin.Enabled = false;
            }
            else
            {
                this.btnLogin.Enabled = true;
            }
        }
        #endregion


        #region "内部方法"
        /// <summary>
        /// 记录密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="isRem"></param>
        private void RememberPwd(string pwd,Boolean isRem)
        {
            if (isRem == true)
            {
                //记住密码
                LoadClientData(MobileServer.ServerID + "pwd", pwd);
            }
            else
            {
                //删除客户端数据
                RemoveClientData(MobileServer.ServerID + "pwd", (object s, ClientDataResultHandlerArgs args) => this.txtPwd.Text = "");
            }
        }

        /// <summary>
        /// 退出登陆
        /// </summary>
        public static void ExitLogin()
        {
            _login_user = null;
            _role_list = new List<BaseRoleModel>();
            _menu_list = new List<BaseMenuDTO>();
        }
        #endregion

        private void btnLogin_Press(object sender, EventArgs e)
        {
            string user = this.txtUser.Text.Trim();
            string pwd = this.txtPwd.Text.Trim();
            try
            {
                if (user.Length <= 0)
                    throw new Exception("请输入用户帐号！");
                if (pwd.Length < 0)
                    throw new Exception("请输入密码！");

                _login_user = this._ctrl.Login(user, pwd);
                this.RememberPwd(pwd, this.checkRemb.Checked);

                if (_login_user != null)
                {
                    BaseUserDTO tmpUserBean = new BaseUserDTO();
                    EntityTools.CopyEntity(_login_user, ref tmpUserBean);
                    Client.Session["User"] = tmpUserBean;
                    _role_list = this._ctrl.GetRoleInfoForUser(_login_user);
                    _menu_list = this._ctrl.GetMenuTreeForRole(_role_list, _prog_info);
                    this.Show(new MainFrm());
                }
                else
                {
                    Toast("登陆失败！");
                }
            }
            catch (Exception ex)
            {
                Toast(ex.Message);
            }
        }

        
    }
}